#include "InputHandler.hpp"

/** Singleton pointer. */
InputHandler* InputHandler::instance = 0;

InputHandler::InputHandler(){
    lastUpdateTime = 0;
    instance = this; //Initialize singleton.
}

/**
 * Get InputHandler singleton.
 */
InputHandler* InputHandler::I(){
    return instance;
}

/**
 * Update input state.
 * @param time Time of update.
 */
void InputHandler::update(unsigned int time){
    lastUpdateTime = time;
    
    mouseX = sf::Mouse::getPosition().y;
    mouseY = sf::Mouse::getPosition().x;

    //Update left button info.
    if(sf::Mouse::isButtonPressed(sf::Mouse::Left)){
        //If left mouse button was not down last update, then it has been pressed.
        if(!leftDown){
            leftPressed = true;
            if(lastLeftReleasedTime < doubleClickSpeed){
                doubleClick = true;
            }
        }else{ //Left mouse button was down last update so it has not been pressed.
            leftPressed = false;
        }
        leftDown = true;
        lastLeftPressedTime = time;
    }else{
        //If left mouse button was down last update then it has been released.
        if(leftDown){
            leftReleased = true;
        }else{
            leftReleased = false;
        }
        leftDown = false;
        lastLeftReleasedTime = time;
    }
    
    if(sf::Mouse::isButtonPressed(sf::Mouse::Right)){
        if(!rightDown){
            rightPressed = true;
        }else{
            rightPressed = false;
        }
        rightDown = true;
        lastRightPressedTime = time;
    }else{
        if(rightDown){
            rightReleased = true;
        }else{
            rightReleased = false;
        }
        rightDown = false;
        lastRightReleasedTime = time;
    }
}

/**
 * Is a keyboard key down.
 * @param key Key being pressed.
 * @return true if pressed, false if not.
 */
bool InputHandler::isKeyPressed(sf::Keyboard::Key key){
    return sf::Keyboard::isKeyPressed(key);
}